<template>
	<view>
		<ScrollView
			class="theme-page-bg padding-20"
			:contentHeight="contentHeight"
		>
			<template #content> 
				<radio-group name="">
					<view class="m-t-20" v-for="(item, index) in list" :key="index" @click="chooseArchives(item)">
						<AddressItem :dataItem="item" @onEdit="onEdit" @onRadioChange="(e) => onRadioChange(index, e)"></AddressItem>
					</view>
				</radio-group>
			</template>
		</ScrollView>
		<view class="button-bottom padding-20 bg-w flex">
			<view class="button-plain" @click="onExit">退出管理</view>
			<view class="button-style flex-1 m-l-20" @click="goTo('/pages/accompany-tmp/my/addArchives/addArchives')">+ 添加档案</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import requestHttp from '@/providers/essential/httpService.js'
	import ScrollView from '@/components/accompany/scrollView/scrollView.vue';
	import AddressItem from '../components/archivesItem/index.vue';
	import api from '@/providers/API/api.js'
	
	import { onLoad ,onShow} from '@dcloudio/uni-app';
	import { ref } from 'vue';
	import type { Ref } from 'vue'
	
	const list = ref([
		{ pic: 'https://img0.baidu.com/it/u=2022043590,1237511079&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500', name: '张三', phone: '15666666666', age: '34' },
		{ pic: 'https://img0.baidu.com/it/u=2022043590,1237511079&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500', name: '李四', phone: '15666666666', age: '13' }
	])
	function goTo(e){
		uni.navigateTo({
			url:e
		})
	}
	const onExit = () => uni.navigateBack();
	
	const onEdit = (item) => {
		uni.navigateTo({url: `/pages/accompany-tmp/my/addArchives/addArchives?detail=${encodeURIComponent(JSON.stringify(item))}`});
	}
	const onRadioChange=(index, e)=>{
		console.log(list.value[index])
		for (let i = 0; i < list.value.length; i++) {
			if(i==index){
				list.value[index].is_default=true
			}else{
				list.value[i].is_default=false
			}
		}
		uni.setStorageSync('archives',e)
	}
	
	const contentHeight:Ref<number> = ref(0)
	const chooseArchives = (item) =>{
		uni.navigateBack(-1)
		uni.setStorageSync('tmpArchives',item)
	}
	onLoad(() => {
		console.log('onLoad')
		let sysInfo = uni.getStorageSync('sysInfo')
		contentHeight.value = sysInfo.screenHeight - sysInfo.statusBarHeight - 44 - 110/sysInfo.devicePixelRatio
		console.log(contentHeight)	
	})
	onShow(()=>{
		const param={
			page:1,
			size:20
		}
		requestHttp.postData(api.searchCaregivers, param).then((res) => {
			list.value=res.data.list
			console.log(list.value)
			for (let i = 0; i < list.value.length; i++) {
				if(list.value[i].id==uni.getStorageSync('archives').id){
					list.value[i].is_default=true
				}else{
					list.value[i].is_default=false
				}
			}
		})
	})
</script>

<style lang="scss" scoped>
	.button-bottom {
		position: fixed;
		height: 100rpx;
		bottom: 0rpx;
		right: 0;
		left: 0;
		view {
			height: 88rpx;
			line-height: 88rpx;
		}
		.button-plain {
			padding: 0 56rpx;
		}
	}
</style>
